/*
    Copyright 2011 Peter Pratscher
 
    This file is part of Surveillance.NET.

    Surveillance.NET is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Surveillance.NET is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Surveillance.NET. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Net.Mail;
using System.Windows.Forms;
using SurveillanceDotNet.Model;

namespace SurveillanceDotNet.AlarmActions
{
    public partial class EMailAlarmActionForm : Form
    {
        public EMailAlarmAction Action { get; set; }

        private Database _Database;

        public EMailAlarmActionForm()
        {
            InitializeComponent();

            _Database = Database.Instance;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            if (tbxName.Text.Length == 0)
            {
                MessageBox.Show("Please enter a name for the action!", "Missing name", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            
            MailAccount account;

            if (cbxEMailAccounts.SelectedIndex == -1)
            {
                MessageBox.Show("Please select an EMail account used for sending the notification messages!", "Missing EMail account", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                account = (MailAccount)cbxEMailAccounts.Items[cbxEMailAccounts.SelectedIndex];
            }

            MailAddress receiverAddress;

            try
            {
                receiverAddress = new MailAddress(tbxSendToAddress.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("The entered EMail address for receiving the notification mails is invalid!", "Invalid EMail address", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            catch (ArgumentException)
            {
                MessageBox.Show("The EMail address for receiving the notification mails cannot be empty!", "Invalid EMail address", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (Action == null)
            {
                Action = new EMailAlarmAction(account, receiverAddress, tbxSubject.Text, tbxMessageContent.Text, chxAttachPictures.Checked);
            }
            else
            {
                Action.EMailAccount = account;
                Action.AttachCameraPictures = chxAttachPictures.Checked;
                Action.MessageText = tbxMessageContent.Text;
                Action.Subject = tbxSubject.Text;
                Action.ToAddress = receiverAddress;
            }

            Action.Name = tbxName.Text;
            Action.Code = tbxCode.Text;

            this.DialogResult = DialogResult.OK;
        }

        private void EMailRegularActionForm_Load(object sender, EventArgs e)
        {
            foreach (MailAccount mc in _Database.MailAccounts)
            {
                cbxEMailAccounts.Items.Add(mc);
            }

            if (Action != null)
            {
                int selectedIndex = -1;

                for (int i = 0; i < _Database.MailAccounts.Count; i++)
                {
                    if (_Database.MailAccounts[i] == Action.EMailAccount)
                    {
                        selectedIndex = i;
                        break;
                    }
                }

                cbxEMailAccounts.SelectedIndex = selectedIndex;

                tbxName.Text = Action.Name;
                tbxCode.Text = Action.Code;
                tbxSendToAddress.Text = Action.ToAddress.Address;
                tbxSubject.Text = Action.Subject;
                tbxMessageContent.Text = Action.MessageText;
                chxAttachPictures.Checked = Action.AttachCameraPictures;
            }
            else
            {
                int exitingActionCounter = 1;

                foreach (AlarmAction action in _Database.AlarmActions)
                {
                    if (action is EMailAlarmAction) exitingActionCounter++;
                }

                tbxName.Text = "Send EMail " + exitingActionCounter;
            }
        }
    }
}
